package com.gt.xmall.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.stereotype.Service;

import com.gt.xmall.service.SysResourceService;




@Service("securityTool")
public class SecurityTool {
	
	@Autowired
	SysResourceService sysResourceService;
	
	
	
	
	public LinkedHashMap getGrantedAuthorityResource(String sysType){
		
		LinkedHashMap resourceMap  = new LinkedHashMap<String, Collection<ConfigAttribute>>();
		List resources =sysResourceService.getGrantedAuthorityResource("1");
		for(int i=0;i<resources.size();i++){
			SysResource mosResource = (SysResource) resources.get(i);
			Collection<ConfigAttribute> attrs = new ArrayList<ConfigAttribute>();
			ConfigAttribute ca = new SecurityConfig("ROLE_ADMIN");
			attrs.add(ca);
			Set set = mosResource.getSysRoles();
			Iterator iterator = set.iterator();
			while(iterator.hasNext()){
				SysRole role = (SysRole)iterator.next();
				if(1 != role.getDelFlag()){
					ca = new SecurityConfig("ROLE_"+role.getRoleId());
					attrs.add(ca);
				}
				
			}
			resourceMap.put(mosResource.getResUrl(), attrs);
		}
		
		
		return resourceMap;
	}
}
